package com.zhu.tool_test.appRunner;

import com.zhu.tool_test.aop.anno.ResultOpre;
import lombok.Data;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanInitializationException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.*;
import org.springframework.stereotype.Component;

import java.lang.reflect.AnnotatedType;
import java.util.Map;
import java.util.Set;

/**
 * @program: tool_test
 * @description: 项目一启动，运行自定义操作
 * @author: ggBall
 * @create: 2020-12-13 14:29
 **/
@Component
@Data
public class myAppRunner implements ApplicationRunner, ApplicationContextAware {

//    @Autowired
//    Scanner scanner;

    private ApplicationContext applicationContext;

    /**
     * Callback used to run the bean.
     *
     * @param args incoming application arguments
     * @throws Exception on error
     */
    @Override
    public void run(ApplicationArguments args) throws Exception {
       // System.out.println("myAppRunner args = " + args);

//        Set<Class<?>> annotationClasses = scanner.getAnnotationClasses("com.zhu.tool_test.controller", ResultOpre.class);
//        for (Class<?> annotationClass : annotationClasses) {
//            Object o = annotationClass.newInstance();
//            System.out.println("o = " + o);
//        }

        Map<String, Object> beansWithAnnotation = applicationContext.getBeansWithAnnotation(ResultOpre.class);
        Set<String> keySet = beansWithAnnotation.keySet();
        for (String s : keySet) {
            Class<?> aClass = beansWithAnnotation.get(s).getClass();
            AnnotatedType[] annotatedInterfaces = aClass.getAnnotatedInterfaces();
            System.out.println("annotatedInterfaces = " + annotatedInterfaces);
        }
        System.out.println("beansWithAnnotation = " + beansWithAnnotation);
    }

    /**
     * Set the ApplicationContext that this object runs in.
     * Normally this call will be used to initialize the object.
     * <p>Invoked after population of normal bean properties but before an init callback such
     * as {@link InitializingBean#afterPropertiesSet()}
     * or a custom init-method. Invoked after {@link ResourceLoaderAware#setResourceLoader},
     * {@link ApplicationEventPublisherAware#setApplicationEventPublisher} and
     * {@link MessageSourceAware}, if applicable.
     *
     * @param applicationContext the ApplicationContext object to be used by this object
     * @throws ApplicationContextException in case of context initialization errors
     * @throws BeansException              if thrown by application context methods
     * @see BeanInitializationException
     */
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }


}
